package array;

import java.util.Arrays;
import java.util.HashSet;

public class leetCode128_longestConsecutive {
    // dp[i] 表示下标为i时的最长连续
    public int longestConsecutive(int[] nums) {
        int max = 0;
        if(nums == null || nums.length == 0) return 0;
        Arrays.sort(nums);
        int n = nums.length;
        int[] dp = new int[n];
        dp[0] = 1;
        for (int i = 1; i < n; i++) {
            if(nums[i] - nums[i - 1] == 1){
                dp[i] = dp[i - 1] + 1;
            }else if(nums[i] - nums[i - 1] == 0){
                dp[i] = dp[i - 1];
            }else{
                dp[i] = 1;
            }
            max = Math.max(dp[i],max);
        }

        return max;

    }

    public int longestConsecutive2(int[] nums) {
        int max = 0;
        if(nums == null || nums.length == 0) return 0;
        if(nums.length == 1) return 1;
        Arrays.sort(nums);
        int n = nums.length;
        int tmp = 1;
        for (int i = 1; i < n; i++) {
            if(nums[i] - nums[i - 1] == 1){
                tmp = tmp + 1;
            }else if(nums[i] - nums[i - 1] == 0){
                tmp = tmp;
            }else{
                tmp = 1;
            }
            max = Math.max(tmp,max);
        }

        return max;
    }
}
